/*
   This class is to be used to display noBusinessBase/form/WidgetForm in a Dialog.
*/


define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dojo/_base/array',
      'dojo/dom-style',
      'dojo/topic',
      'dijit/Dialog'
   ],
   function(declare, lang, dojoArray, domStyle, topic, Dialog)
   {
      return declare([Dialog],
      {
         mSignalNameOnClose: null,
         mWidgetForm: null,
         constructor: function(args)
         {
            // Call Super
            dojo.safeMixin(this, args);
         },
         postCreate: function(args)
         {
            this.inherited(arguments);
            dojo.connect(this, "onHide", this, function(e) {
               lang.hitch(this, setTimeout(lang.hitch(this, function(){
                  this.destroyRecursive();
               }), 50));
            });

            this.attr('content', this.mWidgetForm);

            // Make sure that the scroll bars show up on edit dialogs if the page is too small
            dojoArray.forEach(this.domNode.children, lang.hitch(this, function(pChildDomNode)
            {
               if(pChildDomNode.className == 'dijitDialogPaneContent')
               {
                  domStyle.set(pChildDomNode, 'overflow', 'auto');
               }
            }));
            
            this.mWidgetForm.on("formExecuted", lang.hitch(this, function(data){
               this.hide();
            }));
            this.mWidgetForm.on("formExecutedButNotSaved", lang.hitch(this, function(data){
               this.hide();
            }));
            
            this.mWidgetForm.on("formCanceled", lang.hitch(this, function(data){
               this.hide();
            }));
      
            this.show();
         },
         onHide: function()
         {
            this.inherited(arguments);
            if(!this.mSignalNameOnClose)
               return;
            
            topic.publish(this.mSignalNameOnClose);
         }
      });
   }
);
